package bll

import (
	"fmt"
	"jgt-oper/src/model"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
)

// Enterprise 企业管理
type Enterprise struct {
	EnterpriseModel   model.IEnterprise   `inject:"IEnterprise"`
	EnterHistoryModel model.IEnterHistory `inject:"IEnterHistory"`
	DictionaryBll     *Dictionary         `inject:""`
	ParkModel         model.IPark         `inject:"IPark"`
}

// QueryPage 查询分页数据
func (a *Enterprise) QueryPage(params *schema.EnterpriseQueryParam, pageIndex, pageSize uint) (int, []*schema.Enterprise, error) {
	return a.EnterpriseModel.QueryPage(params, pageIndex, pageSize)
}

// QueryMobilePage 查询手机端分页数据
func (a *Enterprise) QueryMobilePage(params schema.EnterpriseMobileQueryParam, pageSize uint) ([]*schema.EnterpriseMobileQueryResult, error) {
	return a.EnterpriseModel.QueryMobilePage(params, pageSize)
}

// QuerySelect 查询选择数据
func (a *Enterprise) QuerySelect(name string) ([]*schema.EnterpriseSelect, error) {
	return a.EnterpriseModel.QuerySelect(name)
}

// Get 精确查询数据
func (a *Enterprise) Get(recordID string) (*schema.Enterprise, error) {
	return a.EnterpriseModel.Get(recordID)
}

// Create 创建数据
func (a *Enterprise) Create(item *schema.Enterprise) error {
	return a.EnterpriseModel.Create(item)
}

// Update 更新数据
func (a *Enterprise) Update(recordID string, info map[string]interface{}) error {
	return a.EnterpriseModel.Update(recordID, info)
}

// Delete 删除数据...目前没有使用此接口！！！
func (a *Enterprise) Delete(recordID string) error {
	return a.EnterpriseModel.Delete(recordID)
}

// QueryCategory 查询企业分类
func (a *Enterprise) QueryCategory() ([]map[string]string, error) {
	items, err := a.DictionaryBll.QueryTree("OPER$#enterprise_category", 1)
	if err != nil {
		return nil, err
	}

	data := make([]map[string]string, len(items))
	for i, item := range items {
		data[i] = map[string]string{
			"record_id": util.TT(item["code"]).String(),
			"name":      util.TT(item["name"]).String(),
		}
	}
	markData, _ := a.QueryEnterpriseMark()
	fmt.Println("enterprise_mark:", markData)

	return data, nil
}

// QueryEnterpriseMark 查询企业的标识(企业、商家)
func (a *Enterprise) QueryEnterpriseMark() ([]map[string]string, error) {
	items, err := a.DictionaryBll.QueryTree("OPER$#enterprise_mark", 1)
	if err != nil {
		return nil, err
	}

	data := make([]map[string]string, len(items))
	for i, item := range items {
		data[i] = map[string]string{
			"record_id": util.TT(item["code"]).String(),
			"name":      util.TT(item["name"]).String(),
		}
	}

	return data, nil
}

// UpdatePanel 更新展板
func (a *Enterprise) UpdatePanel(recordID string, isOpen int) error {
	info := map[string]interface{}{
		"is_panel": isOpen,
	}
	return a.EnterpriseModel.Update(recordID, info)
}

// ServiceUpdate 更新企业信息(服务平台)
func (a *Enterprise) ServiceUpdate(recordID string, updateParams *schema.ServiceUpdateParams) error {
	info := util.StructToMap(updateParams)
	if info["contacter"] == "" {
		delete(info, "contacter")
	}
	if info["contact_tel"] == "" {
		delete(info, "contact_tel")
	}
	if info["address"] == "" {
		delete(info, "address")
	}
	if info["introduction"] == "" {
		delete(info, "introduction")
	}
	return a.EnterpriseModel.Update(recordID, info)
}
